package com.dferreira.languagesteach.data_parsers;

import com.dferreira.languagesteach.data_models.Language;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;

public class XMLLanguagesParser extends DefaultHandler {
    private final ArrayList<Language> languages;

    /**
     * Starts the variables to the languages parsing
     */
    public XMLLanguagesParser() {
        super();
        languages = new ArrayList<Language>();
    }

    /**
     * Called when tag starts
     *
     * @param uri        The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
     * @param localName  The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @param qName      The qualified name (with prefix), or the empty string if qualified names are not available.
     * @param attributes The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
     */
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) {
        String LANGUAGE_TAG = "language";
        String PREFIX_ATTRIBUTE = "prefix";

        if (localName.equals(LANGUAGE_TAG)) {
            Language language;
            language = new Language();
            language.setPrefix(attributes.getValue(PREFIX_ATTRIBUTE));
            languages.add(language);
        }
    }

    /**
     * @return an array of languages that exist in the system
     */
    public ArrayList<Language> getLanguages() {
        return languages;
    }
}
